package com.macasa.panel.entity.impl;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Version;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import com.google.common.base.Objects;
import com.macasa.panel.common.utils.CastHelpers;
import com.macasa.panel.entity.api.AbstractModel;
import com.macasa.panel.entity.api.IRole;
import com.macasa.panel.entity.api.IUser;

@Entity
@Table(name = "user")
@SequenceGenerator(name = "User_SEQ", sequenceName = "UserIdSeq", initialValue = 1)
@Getter
@Setter
@ToString
public class User extends AbstractModel<Integer> implements IUser {

	/**
	 * The Constant serialVersionUID
	 */
	@Version
	private static final long	serialVersionUID	= 1091300736391087270L;

	@Id
	@Column(name = "id", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "User_SEQ")
	private Integer				id;

	@Column(name = "login", nullable = false, unique = true)
	private String				login;

	@Column(name = "password", nullable = false)
	private String				password;

	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = com.macasa.panel.entity.impl.Role.class)
	@JoinColumn(name = "fk_role", nullable = false)
	private IRole				currentRole;

	@Column(name = "enabled", nullable = false)
	private boolean				enabled;

	@Column(name = "accountNonExpired", nullable = false)
	private boolean				accountNonExpired;

	@Column(name = "credentialsNonExpired", nullable = false)
	private boolean				credentialsNonExpired;

	@Column(name = "accountNonLocked", nullable = false)
	private boolean				accountNonLocked;

	@Override
	public boolean equals(final Object obj) {

		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof User)) {
			return false;
		}

		final User other = CastHelpers.cast(obj, User.class);

		return Objects.equal(this.id, other.id) && Objects.equal(this.login, other.login) && Objects.equal(this.password, other.password)
				&& Objects.equal(this.currentRole, other.currentRole) && Objects.equal(this.enabled, other.enabled)
				&& Objects.equal(this.accountNonExpired, other.accountNonExpired) && Objects.equal(this.credentialsNonExpired, other.credentialsNonExpired)
				&& Objects.equal(this.accountNonLocked, other.accountNonLocked);
	}

	@Override
	public int hashCode() {
		return Objects.hashCode(this.id, this.login, this.password, this.currentRole, this.enabled, this.accountNonExpired, this.credentialsNonExpired,
				this.accountNonLocked);
	}
}
